<?php

namespace App\Kuafu\Order\Service;

use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Order\Model\OrderDetailModel;
use App\Kuafu\Order\Model\OrderModel;
use App\Kuafu\Order\Model\OrderSplitModel;
use Illuminate\Support\Facades\Redis;

/**
 * 订单操作基础服务
 *
 * Class OrderOperateService
 * @package App\Kuafu\Order\Service
 */
class OrderBaseService extends BaseService
{
    /**
     * @var string OrderId
     */
    public string $orderId = '';

    /**
     * 获取订单 ID
     *
     * @return string
     */
    public function getOrderId()
    {
        return $this->orderId;
    }

    /**
     * @var object  主订单详情
     */
    public ?object $orderInfo = null;

    public function __construct($brandId = 0, $shopId = 0, $orderId = '')
    {
        parent::__construct($brandId, $shopId);
        if (!empty($orderId)) {
            $this->orderId   = strval($orderId);
            $this->orderInfo = $this->getOrderInfo(true);
        }
    }

    /**
     * 获取订单详情
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getOrderInfo(bool $refresh = false)
    {
        return $refresh
            ? OrderSplitModel::suffix($this->shopId)
                ->newQuery()
                ->where('orderId', $this->orderId)
                ->first()
            : $this->orderInfo;
    }

    /**
     * @var array 获取订单详情
     */
    private static array $orgDetail = [];

    public function getOrderDetail(bool $refresh = false): array
    {
        if (empty(self::$orgDetail[$this->orderId]) || $refresh) {
            $orderDetail = (new OrderDetailModel())->query()->where('orderId', $this->orderId)->get();

            self::$orgDetail[$this->orderId] = $orderDetail->toArray() ?? [];
        }
        return self::$orgDetail[$this->orderId];
    }

    /**
     * @var array 获取原订单的美团优惠券信息
     */
    private static array $orgExtCoupon = [];

    public function getOrderExtCoupon(bool $refresh = false): array
    {
//        if (empty(self::$orgExtCoupon[$this->orderId]) || $refresh) {
//
//            $orderDetail = (new OrderCouponExtModel())->query()->where('orderId', $this->orderId)
//                                                      ->where('couponStatus', OrderCouponExtModel::COUPON_STATUS_1)
//                                                      ->get();
//
//            self::$orgExtCoupon[$this->orderId] = $orderDetail->toArray() ?? [];
//
//        }
//        return self::$orgExtCoupon[$this->orderId];
    }

    /**
     * 订单上锁
     * @param int $time
     * @param string $tag
     * @return mixed
     */
    public function lockOrder($time = 0, $tag = 'reserve')
    {
        $key    = $this->getOrderLockPayKey($tag);
        $return = Redis::setNx($key, 'locked');

        if (!empty($time)) {
            Redis::expire($key, $time);
        }

        return $return;
    }

    /**
     * 解锁订单
     */
    public function unLockOrder($tag = 'reserve')
    {
        $key    = $this->getOrderLockPayKey($tag);
        $return = Redis::del($key);

        return $return;
    }

    /**
     * 获取锁单的 key
     * @return string
     */
    public function getOrderLockPayKey($tag = 'reserve')
    {
        return 'REDIS:LOCK_ORDER:' . $tag . ':' . $this->brandId . '_' . $this->shopId . ':' . $this->orderId;
    }

    /**
     * 更新订单状态
     *
     * @param $orderStatus
     */
    public function updateOrderStatus($orderStatus)
    {
        $this->orderInfo->orderStatus = $orderStatus;
        $this->orderInfo->save();
    }
}
